﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Tdb.YY.DTO;
using Tdb.YY.DTO.YY;
using Tdb.YY.Helper.Config;
using Tdb.YY.Model;

namespace Tdb.YY.DAL
{
    /// <summary>
    /// YY1 数据层
    /// </summary>
    public class YY1DAL : BaseDAL
    {
        #region 单例

        /// <summary>
        /// 构造函数
        /// </summary>
        private YY1DAL()
        {
        }

        private static object _lock = new object();
        private static YY1DAL _inst = null;
        /// <summary>
        /// 单例
        /// </summary>
        public static YY1DAL Inst
        {
            get
            {
                if (_inst == null)
                {
                    lock (_lock)
                    {
                        if (_inst == null)
                        {
                            _inst = new YY1DAL();
                        }
                    }
                }

                return _inst;
            }
        }

        #endregion

        #region 公开方法

        /// <summary>
        /// 查询YY1类型
        /// </summary>
        /// <returns></returns>
        public async Task<List<string>> QueryCTypeAsync()
        {
            using (SysContext db = new SysContext())
            {
                var list = await db.YY1s.GroupBy(m => m.CType).Select(m => m.Key).ToListAsync();
                return list;
            }
        }

        /// <summary>
        /// 查询YY1列表
        /// </summary>
        /// <param name="req">条件</param>
        /// <returns></returns>
        public async Task<PageList<YY1>> QueryListAsync(YYListReq req)
        {
            using (SysContext db = new SysContext())
            {
                var q = db.YY1s.AsNoTracking();

                //分类
                if (string.IsNullOrWhiteSpace(req.CType) == false)
                {
                    q = q.Where(m => m.CType == req.CType);
                }

                //权重
                q = q.OrderByDescending(m => m.Right);

                return await this.QueryPageList<YY1>(q, req);
            }
        }

        /// <summary>
        /// 过滤YY1列表
        /// </summary>
        /// <param name="req">条件</param>
        /// <returns></returns>
        public async Task<PageList<YY1>> FilterListAsync(YYFilterListReq req)
        {
            using (SysContext db = new SysContext())
            {
                var q = db.YY1s.AsNoTracking();

                //标题
                if (string.IsNullOrWhiteSpace(req.Title) == false)
                {
                    q = q.Where(m => m.Title.Contains(req.Title));
                }

                //内容
                if (string.IsNullOrWhiteSpace(req.Content) == false)
                {
                    q = q.Where(m => m.Content.Contains(req.Content));
                }

                //权重
                q = q.OrderByDescending(m => m.Right);

                return await this.QueryPageList<YY1>(q, req);
            }
        }

        /// <summary>
        /// 查询YY1信息
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        public async Task<YY1> GetByIDAsync(int id)
        {
            using (SysContext db = new SysContext())
            {
                var model = await db.YY1s.AsNoTracking().FirstOrDefaultAsync(m => m.ID == id);

                return model;
            }
        }

        /// <summary>
        /// 查询异常记录url列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<string>> QueryErrorUrlAsync()
        {
            using (SysContext db = new SysContext())
            {
                //查询内容长度过小的记录
                var q = db.YY1s.AsNoTracking().Where(m => m.Content.Length < SysJson.Inst.AppConfig.MinContentLen);

                var list = await q.Select(m => m.Url).ToListAsync();

                return list;
            }
        }

        /// <summary>
        /// 删除异常记录
        /// </summary>
        /// <returns></returns>
        public async Task<int> DeleteErrorInfoAsync()
        {
            using (SysContext db = new SysContext())
            {
                //获取内容长度过小的记录
                var list = await db.YY1s.Where(m => m.Content.Length < SysJson.Inst.AppConfig.MinContentLen).ToListAsync();

                //删除内容长度过小的记录
                db.YY1s.RemoveRange(list);

                var count = await db.SaveChangesAsync();

                return count;
            }
        }

        /// <summary>
        /// 标题重复的记录去重
        /// </summary>
        /// <returns></returns>
        public async Task<int> DistincTitleAsync()
        {
            using (SysContext db = new SysContext())
            {
                //按标题分组，获取组内第一条记录的ID
                var lstID = await db.YY1s.AsNoTracking().GroupBy(m => m.Title).Where(m => m.Key.Length > 10 && m.Count() > 1).Select(m => m.First().ID).ToListAsync();

                //获取记录
                var list = await db.YY1s.Where(m => lstID.Contains(m.ID)).ToListAsync();

                //删除记录
                db.YY1s.RemoveRange(list);

                var count = await db.SaveChangesAsync();

                return count;
            }
        }

        /// <summary>
        /// 修改权重
        /// </summary>
        /// <param name="req">条件</param>
        /// <returns></returns>
        public async Task ModifyRightAsync(YYModifyRightReq req)
        {
            using (SysContext db = new SysContext())
            {
                var model = await db.YY1s.FirstOrDefaultAsync(m => m.ID == req.ID);
                if (model == null)
                {
                    return;
                }

                model.Right += req.Offset;

                db.Entry(model).State = EntityState.Modified;
                await db.SaveChangesAsync();
            }
        }

        #endregion
    }
}
